efform_onload = function (){
	var year=$('#inqu_status-0-fdYear',window.parent.document).val();
	var month=$('#inqu_status-0-fdMonth',window.parent.document).val();
	var fdDepartmentId=$('#inqu_status-0-fdDepartmentId',window.parent.document).val();
	$('#inqu_status-0-fdYear').val(year);
	$('#inqu_status-0-fdMonth').val(month);
	$('#inqu_status-0-fdDepartmentId').val(fdDepartmentId);
}; 
button_insert_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	var info=_getEi();
	var eiBlock=info.getBlock("cmes.cn.custom");
	for(var i=0;i<rows.length;i++)
	{
		var place=grid.getCellValueByColumnName(rows[i],"fdPlaceId");  
		var custom=grid.getCellValueByColumnName(rows[i],"fdTargetDepartmentId");
		for(var j=0;j<eiBlock.getRows().length;j++)
		{
		   var _value = eiBlock.getCell(j,"fdKeyId");
		   if(_value==custom)
		   {	
			  custom=eiBlock.getCell(j,"fdShortName");
		   }
		}
		if(place==custom)
		{
			EFAlert("貌似有问题哦。地点和客户名称相同了哦。这样是不行的，请检查一下。"); 
			return;
		}
	}
//	efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","insert",true);
	var info = getSCZXEiInfos("ZMCN54","ef_grid_r");//ZMCN10 为EFFORM id，ef_grid_r为grid id（paintId）
	if (null != info) { 
		EiCommunicator.send("ZMCN54", "validate", info, validate_callback);
	}
}
var validate_callback={
		onSuccess:function(eiInfo){
			if(eiInfo.getStatus()==-1){
				EFAlert("操作失败，原因："+eiInfo.getMsg());
			}else{
/*				 efwindow.hide();
				 var grid = efgrid.getGridObject("ef_grid_d");
				 grid.refresh(eiInfo);*/
				var oper="insert";
				if(eiInfo.get("efCurButtonEname")=="UPDATE"){
					oper="update";
				}
				efgrid.submitForm("ef_grid_r", "ZM", "ZMCN54",oper, true);
			}
		},
		onFail:function(eMsg)
		{
			EFAlert("操作失败，原因："+eMsg); 
		}
}
button_update_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	var info=_getEi();
	var eiBlock=info.getBlock("cmes.cn.custom");
	for(var i=0;i<rows.length;i++)
	{
/*		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="草拟")
		{
			EFAlert("只能修改草拟状态的记录！"); return;
		}*/
		var place=grid.getCellValueByColumnName(rows[i],"fdPlaceId");  
		var custom=grid.getCellValueByColumnName(rows[i],"fdTargetDepartmentId");
		for(var j=0;j<eiBlock.getRows().length;j++)
		{
		   var _value = eiBlock.getCell(j,"fdKeyId");
		   if(_value==custom)
		   {	
			  custom=eiBlock.getCell(j,"fdShortName");
		   }
		}
		if(place==custom)
		{
			EFAlert("貌似有问题哦。地点和客户名称相同了哦。这样是不行的，请检查一下。"); 
			return;
		}
	}
//	efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","update",true);
	var flag=0;//1表示草拟
	var flag2=0;//1表示上报
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="草拟")
		{
			flag2=1;
		}else{
			flag=1;
		}
	}
	if(flag==1 && flag2==1){
		//既勾选了草拟又勾选了生效的记录，提示禁止操作
		EFAlert("批量操作只能针对相同状态的记录！不能同时包含“草拟”和“上报”的记录"); return;
	}else if(flag==1){
		//全部草拟记录，逐条判断是不是将日期修改为了历史日期，如果将日期改为比最大生效日期还早的记录，提示禁止操作
//		efgrid.submitForm( "ef_grid_r", "ZM","ZMCN51","update",true);
		var info = getSCZXEiInfos("ZMCN54","ef_grid_r");//ZMCN10 为EFFORM id，ef_grid_r为grid id（paintId）
		if (null != info) { 
			EiCommunicator.send("ZMCN54", "validate", info, validate_callback);
		}
	}else{
		//全部生效记录，检验后操作
		var info = getSCZXEiInfos("ZMCN54","ef_grid_r");//ZMCN10 为EFFORM id，ef_grid_r为grid id（paintId）
		if (null != info) { 
			EiCommunicator.send("ZMCN54", "validate", info, validate_callback);
		}
	}
}

button_delete_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdStatus")!="草拟")
		{
			EFAlert("只能删除草拟状态的记录！"); return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","delete",true);
	}); 
}
button_report_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	/*	if(rows.length>1){
		EFAlert("为了保证库存以及调整量累计值计算正确，请每次勾选一条记录上报！"); return;
	}*/
	if(grid.getCellValueByColumnName(rows[0],"fdStatus")!="草拟")
	{
		EFAlert("该记录已经上报！"); return;
	}
	EFConfirm('你确认要上报吗?', '确认框', function(r) {
//		if(r)efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","report",true);
		if(r){
			efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","report",true);
/*			var info = getSCZXEiInfos("ZMCN54","ef_grid_r");//ZMCN10 为EFFORM id，ef_grid_r为grid id（paintId）
			if (null != info) { 
				EiCommunicator.send("ZMCN54", "validate1", info, validate1_callback);
			}*/
		}
	}); 
}
var validate1_callback={
		onSuccess:function(eiInfo){
			if(eiInfo.getStatus()==-1){
				EFAlert(eiInfo.getMsg());
			}else{
				efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","report",true);
			}
		},
		onFail:function(eMsg)
		{
			EFAlert("操作失败，原因："+eMsg); 
		}
}
button_revert_onclick = function (){
	var grid=isSelected("ef_grid_r");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	/*	if(rows.length>1){
		EFAlert("为了保证库存以及调整量累计值计算正确，请每次勾选一条记录退回！"); return;
	}*/
	if(grid.getCellValueByColumnName(rows[0],"fdStatus")=="草拟")
	{
		EFAlert("该记录是草拟记录，无需退回！提示：只能退回已经上报的记录"); return;
	}
	EFConfirm('你确认要退回吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","revert",true);
/*			var info = getSCZXEiInfos("ZMCN54","ef_grid_r");//ZMCN10 为EFFORM id，ef_grid_r为grid id（paintId）
			if (null != info) { 
				EiCommunicator.send("ZMCN54", "validate2", info, validate2_callback);
			}*/
		}
	}); 
}
var validate2_callback={
		onSuccess:function(eiInfo){
			if(eiInfo.getStatus()==-1){
				EFAlert(eiInfo.getMsg());
			}else{
				efgrid.submitForm( "ef_grid_r", "ZM","ZMCN54","revert",true);
			}
		},
		onFail:function(eMsg)
		{
			EFAlert("操作失败，原因："+eMsg); 
		}
}
efgrid_afterAddNewRow = function(grid_id) {
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	if (grid_id == "ef_grid_r") {
		for (var i = 0; i < rows.length; i++) {
			grid.getBlockData().setCell(rows[i], "fdStatus", "草拟");
			grid.getBlockData().setCell(rows[i], "fdDepartment", _getEi().get('fdDepartment'));
			grid.getBlockData().setCell(rows[i], "fdCurrentDate", getCurrentDate());		
		}
	}
	for (var i = 0; i < rows.length; i++) {
		grid.getBlockData().setCell(rows[i], "fdKeyId", "");
	}
}
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	var grid = efgrid.getGridObject( grid_id ); 
	if( col_index == 4){ 
		//修改了煤样编码，重新截取日期
		if(cell_value.length>=10){
			cell_value=cell_value.substring(2,6)+"-"+cell_value.substring(6,8)+"-"+cell_value.substring(8,10);
		}else{
			EFAlert("煤样编码错，请重新输入煤样编码");
			cell_value=getCurrentDate();
		}
		grid.getBlockData().setCell(row_index, "fdCurrentDate",cell_value);
		grid.refreshCell( row_index, 1 , TYPE_DATA );
	}else if( col_index == 11){ 
		//修改了MJ/kg，重新计算kcal/kg
		if(cell_value!=null){
			cell_value=cell_value/4.1816*1000;
			cell_value=cell_value.toFixed(0);
		}
		grid.getBlockData().setCell(row_index, "fdMyQnetk",cell_value);
		grid.refreshCell( row_index, 12 , TYPE_DATA );
	}
}